package _07_串;


/**
 *  3 百
 *  4 千
 *  5 10 千
 *  3 1 2 3
 *  1234567891
 * 输出："One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
 *
 */

public class _273_整数转换英文表示 {
    // 1 - 9
    String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};

    // 10 - 19
    String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};

    // 10 20 30 40 50 60 70 80 90
    String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

    // 百 千 百万 十亿
    // 3  4  6
    String[] thousands = {"", "Thousand", "Million", "Billion"};

    // 1000000000

    // 每三位一组, 处理Billion是单独算一位，其他都是三位一组
    public String numberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 3, div = 1000000000; i >= 0; i++, div /= 1000) {
            int curNum = num / div;
            if (curNum != 0) {
                num -= curNum * div;
                StringBuilder curr = new StringBuilder();
                recursion(curr, curNum);
                curr.append(thousands[i]).append(" ");
                sb.append(curr);
            }
        }
        return sb.toString().trim();
    }

    public void recursion(StringBuilder curr, int num) {
        if (num == 0) {
            return;
        } else if (num < 10) {
            curr.append(singles[num]).append(" ");
        } else if (num < 20) {
            curr.append(teens[num - 10]).append(" ");
        } else if (num < 100) {
            curr.append(tens[num / 10]).append(" ");
            recursion(curr, num % 10);
        } else {
            curr.append(singles[num / 100]).append(" Hundred ");
            recursion(curr, num % 100);
        }
    }
}
